package org.mozhu.learning.algo.binary_search;

public class BinarySearchV2 implements BinarySearch {

    @Override
    public int binarySearch(int[] a, int value) {
        return searchInternally(a, 0, a.length - 1, value);
    }

    private int searchInternally(int[] a, int low, int high, int value) {
        if (low > high || a[low] > value || a[high] < value) return -1;
        int mid = low + ((high - low) >> 1);
        if (a[mid] == value) {
            return mid;
        } else if (a[mid] > value) {
            return searchInternally(a, low, mid - 1, value);
        } else {
            return searchInternally(a, mid + 1, high, value);
        }
    }
}
